<?php
if (!defined('IN_RJ')) die("Error 404");
header("Content-type: image/png; charset=utf-8");
function imagettftextbox($image, $size, $angle, $left, $top, $color, $font, $text, $max_width, $align){
    $text_lines = explode("\n", $text); // Supports manual line breaks!
    $lines = array();
    $line_widths = array();
    $largest_line_height = 0;
    foreach($text_lines as $block) {
        $current_line = ''; // Reset current line
        $words = explode(' ', $block); // Split the text into an array of single words
        $first_word = TRUE;
        $last_width = 0;
        for($i = 0; $i < count($words); $i++) {
            $item = $words[$i];
            $dimensions = imagettfbbox($size, $angle, $font, $current_line . ($first_word ? '' : ' ') . $item);
            $line_width = $dimensions[2] - $dimensions[0];
            $line_height = $dimensions[1] - $dimensions[7];
            if($line_height > $largest_line_height) $largest_line_height = $line_height;
                if($line_width > $max_width && !$first_word) {
                    $lines[] = $current_line;
                    $line_widths[] = $last_width ? $last_width : $line_width;
                    $current_line = $item;
                } else {
                    $current_line .= ($first_word ? '' : ' ') . $item;
                }
                if($i == count($words) - 1) {
                    $lines[] = $current_line;
                    $line_widths[] = $line_width;
                }
                $last_width = $line_width;
                $first_word = FALSE;
            }
            if($current_line) {
                $current_line = $item;
            }
        }
        $i = 0;
        foreach($lines as $line) {
            if($align == 'CENTER') {
                $left_offset = ($max_width - $line_widths[$i]) / 2;
            } elseif($align == 'RIGHT') {
                $left_offset = ($max_width - $line_widths[$i]);
            }

            imagettftext($image, $size, $angle, $left + $left_offset, $top + $largest_line_height + ($largest_line_height * $i), $color, $font, $line);
            $i++;
        }
        return $largest_line_height * count($lines);
}

if($id = (int)$_GET['id']) {
	$r = $DB->fetch_row($DB->query("SELECT m_title, m_singer, m_multimedia FROM ".$conf['prefix']."media WHERE m_id = ".$id));
	$title = $func->unhtmlchars($r['m_title']);
	$singer = $func->unhtmlchars($func->get_data('singer',$r['m_singer']));
	$album = $func->unhtmlchars($func->get_data('multimedia',$r['m_multimedia']));
	$Image_Width=300;
	$Text_Size= 12;
	$Image_Height=130;
	$img = @imagecreatetruecolor($Image_Width, $Image_Height)
	    or die($lang['error']);
	$background_color = imagecolorallocate($img, 0, 0, 0);
	imagefill($img, 0, 0, $background_color);
	$text_color = imagecolorallocate($img, 255, 255, 255);
	$font = "source/Tahoma.ttf";
	$head = "wWw.360Phim.Info";
	$text = "\n\n{$lang['song']}: $title\n{$lang['singer']}: $singer\n{$lang['album']}: $album";
	imagettftextbox($img, 15, 0, 0, 10, $text_color, $font, $head, $Image_Width,'CENTER');
	imagettftextbox($img, $Text_Size, 0, 0, 10, $text_color, $font, $text, $Image_Width,'CENTER');
	imagepng($img);
	imagedestroy($img);
}elseif($id = (int)$_GET['avatar']) {
	if(!$cached['USER']['avatar'][$id]){
		$r = $mysql->fetch_array($mysql->query("SELECT user_avatar FROM ".$tb_prefix."user WHERE user_id = ".$_SESSION['user_id']));
		$cached['USER']['avatar'][$id] = base64_decode($r['user_avatar']);
	}
	echo $cached['USER']['avatar'][$id];
}elseif($_GET['security']){
	$im = @imagecreatetruecolor(70, 20)
    	or die($lang['error']);
	$background_color = imagecolorallocate($im, 255, 255, 255);
	imagefill($im, 0, 0, $background_color);
	$text_color = imagecolorallocate($im, 233, 14, 91);
	$_SESSION['sec_num_img'] = rand(1000000,9999999);
	imagestring($im, 5, 5, 5,  $_SESSION['sec_num_img'], $text_color);
	imagepng($im);
	imagedestroy($im);
}
?> 